package com.yc.sole.financial.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.financial.dto.PieceRateWageGroupDto;
import com.yc.sole.financial.entity.PieceRateWageGroup;
import com.yc.sole.financial.mapper.PieceRateWageGroupMapper;
import com.yc.sole.financial.request.PieceRateWageGroupQueryReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-03-20
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class PieceRateWageGroupRepository extends ServiceImpl<PieceRateWageGroupMapper, PieceRateWageGroup> {

    /**
     * 新增
     *
     * @param pieceRateWageProcessDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public Long savePieceRateWageProcess(PieceRateWageGroupDto pieceRateWageProcessDto) {
        PieceRateWageGroup entity = new PieceRateWageGroup();
        BeanUtil.copyProperties(pieceRateWageProcessDto, entity);
        this.save(entity);
        return entity.getId();
    }

    /**
     * 通过Id修改
     *
     * @param pieceRateWageProcessDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updatePieceRateWageProcessById(PieceRateWageGroupDto pieceRateWageProcessDto) {
        PieceRateWageGroup entity = new PieceRateWageGroup();
        BeanUtil.copyProperties(pieceRateWageProcessDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deletePieceRateWageProcessByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<PieceRateWageGroupDto> selectPieceRateWageProcessByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), PieceRateWageGroupDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public PieceRateWageGroupDto selectPieceRateWageProcessById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), PieceRateWageGroupDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<PieceRateWageGroupDto> selectPieceRateWageProcessPage(Paging page, PieceRateWageGroupQueryReq queryReq) {
        LambdaQueryWrapper<PieceRateWageGroup> queryWrapper = Wrappers.<PieceRateWageGroup>lambdaQuery()
                .eq(Objects.nonNull(queryReq.getShiftId()) && queryReq.getShiftId() > 0, PieceRateWageGroup::getShiftId, queryReq.getShiftId())
                .eq(Objects.nonNull(queryReq.getProcessId()) && queryReq.getProcessId() > 0, PieceRateWageGroup::getProcessId, queryReq.getProcessId())
                .eq(CharSequenceUtil.isNotBlank(queryReq.getDate()), PieceRateWageGroup::getDate, queryReq.getDate())
                .orderByDesc(PieceRateWageGroup::getDate, PieceRateWageGroup::getProcessId)
                .and(CharSequenceUtil.isNotBlank(queryReq.getKeyword()),
                        w->w.like(PieceRateWageGroup::getDate,queryReq.getKeyword()));
        if (CollUtil.isNotEmpty(queryReq.getDateRange())
                && CharSequenceUtil.isNotBlank(queryReq.getDateRange().get(0))
                && CharSequenceUtil.isNotBlank(queryReq.getDateRange().get(1))) {
            queryWrapper.between(PieceRateWageGroup::getDate, queryReq.getDateRange().get(0), queryReq.getDateRange().get(1));
        }
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), PieceRateWageGroupDto.class);
    }

    /**
     * 根据工序、班次、日期删除
     *
     * @param shiftId
     * @param processId
     * @param date
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByShiftIdProcessIdDate(Long shiftId, Long processId, String date) {
        LambdaQueryWrapper<PieceRateWageGroup> wrapper = Wrappers.<PieceRateWageGroup>lambdaQuery()
                .eq(PieceRateWageGroup::getShiftId, shiftId)
                .eq(PieceRateWageGroup::getProcessId, processId)
                .eq(PieceRateWageGroup::getDate, date);
        this.remove(wrapper);
    }
}
